/* Original Author:  Dominick O'Dierno
 * Date: 2007   odiernod@gmail.com     --Originally intended for use with DOMercury:  http://www.odierno.com/domercury
 * Description: Handles Options UI for folder indexing
 *
 * --CHANGELOG--
 * 2008-05-18 - Tao Klerks:
 *  - Added "Special Folder" handling for internationalization etc
 *  - Added "Include Folder" option
 *  - Added "Include Subfolders" option
 * 
 * */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FolderIndexorPlugin
{
    public partial class IFPanel : UserControl
    {
        public IFPanel(IndexedFolder InF)
        {
            InitializeComponent();
            cmb_SpecialPaths.DataSource = Enum.GetNames(typeof(IndexedFolder.SpecialFolderType));
            txtPath.Text = InF.FolderPath;
            chkSD.Checked = InF.SearchSubFolders;
            txtSP.Text = InF.SearchPattern;
            chkfolder.Checked = InF.IncludeFolderItem;
            chkSubFolderItems.Checked = InF.IncludeSubFolderItems;
            rb_Path.Checked = !InF.UseSpecialFolder;
            rb_Special.Checked = InF.UseSpecialFolder;
            for (int i = 0; i < cmb_SpecialPaths.Items.Count; i++)
            {
                if (cmb_SpecialPaths.Items[i].ToString() == InF.SpecialFolder.ToString())
                {
                    cmb_SpecialPaths.SelectedIndex = i;
                }
            }
            updateDependencyDisplay();
        }


        public IFPanel()
        {
            InitializeComponent();
            cmb_SpecialPaths.DataSource = Enum.GetNames(typeof(IndexedFolder.SpecialFolderType));
            updateDependencyDisplay();
        }

        public IndexedFolder IndexedFolder
        {
            get
            {
                IndexedFolder.SpecialFolderType selectedType = IndexedFolder.SpecialFolderType.Documents;
                if (cmb_SpecialPaths.SelectedItem != null)
                    selectedType = (IndexedFolder.SpecialFolderType)Enum.Parse(typeof(IndexedFolder.SpecialFolderType), cmb_SpecialPaths.SelectedItem.ToString());

                bool useSpecialFolder = false;
                if (rb_Special.Checked)
                    useSpecialFolder = true;

                return new IndexedFolder(txtPath.Text.Trim(), chkSD.Checked, chkfolder.Checked, chkSubFolderItems.Checked, txtSP.Text.Trim(), useSpecialFolder, selectedType);
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    txtPath.Text = dlg.SelectedPath;
                }
            }
        }
        private void updateDependencyDisplay()
        {
            if (this.chkSD.Checked)
                chkSubFolderItems.Enabled = true;
            else
                chkSubFolderItems.Enabled = false;

            if (this.rb_Path.Checked)
            {
                txtPath.Enabled = true;
                btnBrowse.Enabled = true;
                cmb_SpecialPaths.Enabled = false;
            }
            else
            {
                txtPath.Enabled = false;
                btnBrowse.Enabled = false;
                cmb_SpecialPaths.Enabled = true;
            }

        }


        private void chkSD_CheckedChanged(object sender, EventArgs e)
        {
            updateDependencyDisplay();
        }

        private void rb_Path_CheckedChanged(object sender, EventArgs e)
        {
            updateDependencyDisplay();
        }

        private void rb_Special_CheckedChanged(object sender, EventArgs e)
        {
            updateDependencyDisplay();
        }

    }
}
